package com.ease.web.webwork;

import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.ease.common.vo.Endpoint;
import com.opensymphony.webwork.ServletActionContext;
import com.opensymphony.xwork.ActionContext;

/**
 * 
 * @author nathanleewei
 * 
 */
public abstract class RequestUtil {

	public static Endpoint getServerEndPoint() {
		HttpServletRequest request = ServletActionContext.getRequest();
		return getServerEndPoint(request);
	}

	public static Endpoint getServerEndPoint(HttpServletRequest request) {

		Endpoint endpoint = new Endpoint();
		endpoint.setName((request.getLocalName() != null) ? request.getLocalName() : null);
		endpoint.setAddress((request.getLocalAddr() != null) ? request.getLocalAddr() : null);
		endpoint.setPort(request.getLocalPort());
		// endpoint.setContext(request.getContextPath());

		return endpoint;
	}

	public static Endpoint getRemoteEndPoint() {
		HttpServletRequest request = ServletActionContext.getRequest();

		Endpoint endpoint = new Endpoint();
		endpoint.setName((request.getRemoteHost() == null) ? request.getRemoteAddr() : request.getRemoteHost());
		endpoint.setAddress(getIpAddress(request));
		endpoint.setPort(request.getRemotePort());
		return endpoint;
	}

	public static String getIpAddress(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}

	@SuppressWarnings("unchecked")
	public static String getParameterString() throws Exception {
		StringBuffer parameterString = new StringBuffer();
		Map<String, Object> parameters = ActionContext.getContext().getParameters();
		if (null != parameters && parameters.size() > 0) {
			//
			Iterator<String> iterator = parameters.keySet().iterator();
			//
			String key = null;
			String[] values = null;
			Object object = null;
			//
			while (iterator.hasNext()) {
				key = (String) iterator.next();
				object = parameters.get(key);
				if (object instanceof String[]) {
					values = (String[]) parameters.get(key);
					for (int i = 0; i < values.length; i++) {
						// url.append(key).append("=").append(values[i]).append(
						// "&");
						parameterString.append(key).append("=").append(URLEncoder.encode(values[i], "UTF-8")).append("&");
					}
				} else {
					parameterString.append(key).append("=").append(String.valueOf(object)).append("&");
				}

			}
		}
		return parameterString.toString();
	}

	public static String getRequestString() throws Exception {
		StringBuffer requestString = ServletActionContext.getRequest().getRequestURL().append("?").append(getParameterString());
		return requestString.toString();
	}

	/** return String: "/action?parameters" */
	public static String getRequestUrl() {
		String pathInfo = ServletActionContext.getRequest().getPathInfo();
		String queryString = ServletActionContext.getRequest().getQueryString();
		return ServletActionContext.getRequest().getServletPath() + (pathInfo != null ? pathInfo : "") + (queryString != null ? "?" + queryString : "");
	}

}
